package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.UserPageRequest;
import com.example.springboot.entity.User;
import com.example.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:User控制层，接收和请求数据
 *
 * @Author 马钰涵
 * @Create 2023/8/4 23:39
 * @Version 1.0
 */
//@CrossOrigin                        //处理跨域方式之一
@RestController                     //指明这是一个接口，所有数据都是Json格式的
@RequestMapping("/user")         //后端父级路由
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 修改信息
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    /**
     * 根据id获取数据
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 根据id删除数据
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        userService.deleteById(id);
        return Result.success();
    }
    /**
     * 批量删除数据
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        userService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 新增接口
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        userService.save(user);
        return Result.success();
    }
    /**
     * 充值接口
     */
    @PostMapping("/account")
    public Result account(@RequestBody User user) {
        userService.addAccount(user);
        return Result.success();
    }


    /**
     * 查询所有数据
     */
    @GetMapping("/list")
    public Result list() {
        return Result.success(userService.list());
    }

    /**
     * 分页模糊查询
     */
    @GetMapping("/page")
    public Result page(UserPageRequest userPageRequest){
        return Result.success(userService.page(userPageRequest));
    }
}
